namespace EyeOpen.Trial.Service.Test
{
	using System;
	using System.Collections.Generic;
	using System.Linq;

	using EyeOpen.Trial.Data;
	using EyeOpen.Trial.Domain;
	using EyeOpen.Trial.Service;

	using Microsoft.VisualStudio.TestTools.UnitTesting;

	using Moq;

	using SharpTestsEx;

	[TestClass]
	public class ApplicationServiceTest
	{
		private const string Serial = "001";

		[TestMethod]
		public void CheckNullSerialIsNotValid()
		{
			InitializeService()
				.IsValid(null)
				.Should("Null serial is classified as valid.")
				.Be
				.False();
		}

		[TestMethod]
		public void CheckNotValidSerialIsNotValid()
		{
			InitializeService()
				.IsValid(Serial)
				.Should("Not existing serial is classified as valid.")
				.Be
				.False();
		}

		[TestMethod]
		public void CheckValidSerialExpiredIsNotValid()
		{
			InitializeService(new Application { Serial = Serial, ExpirationDate = DateTime.Now.AddDays(15) })
				.IsValid(Serial)
				.Should("Valid serial but expired is classified as valid.")
				.Be
				.False();
		}

		[TestMethod]
		public void CheckValidSerialIsValid()
		{
			InitializeService(new Application { Serial = Serial, ExpirationDate = DateTime.Now })
				.IsValid(Serial)
				.Should("Valid serial is classified as not valid.")
				.Be
				.True();
		}

		private ApplicationService InitializeService(params Application[] applicationCollection)
		{
			var applicationContext = new Mock<IApplicationUnitOfWork>();

			var applicationRepository = GetRepository(applicationCollection);

			applicationContext
				.SetupGet(x => x.ApplicationRepository)
				.Returns(applicationRepository.Object);

			return new ApplicationService(applicationContext.Object);
		}

		private Mock<IRepository<Application>> GetRepository(params Application[] applicationCollection)
		{
			var applicationRepository = new Mock<IRepository<Application>>();

			var list = 
				new List<Application>(applicationCollection)
					.AsQueryable();

			applicationRepository
				.SetupGet(x => x.Provider)
				.Returns(list.Provider);

			applicationRepository
				.SetupGet(x => x.Expression)
				.Returns(list.Expression);

			return applicationRepository;
		}
	}
}